const express = require('express');

// 业务层
const devService = require('../service/dev');

// 实体类
const ArgvVersion = require('../entity/dev/ArgvVersion');
const DevType = require('../entity/dev/DevType');
const ArgvVersionCopy = require('../entity/dev/ArgvVersionCopy');
const Device = require('../entity/dev/Device');
const UpdateDevInfo = require('../entity/dev/UpdateDevInfo');
const UpdateOpenRate = require('../entity/dev/UpdateOpenRate');
const Response = require('../entity/http/Response');
const ObjId = require('../entity/common/ObjId');
const SafeString = require('../entity/base/SafeString');

const router = express.Router();

/**
 * @apiName 创建参数版本接口
 * @param { string } version     参数版本名称
 * @param { int }    count       有效参数个数
 * @param { string } type        型号系列
 * @param { string } content     参数内容 - JSON字符串 - [{ id: "序号", type: "参数类型", name: "参数名称", unit: "单位" }, ...]
 */
router.post('/argv-version', function (req, res, next) {
    console.log(`[ REQUEST ] [ req-id = ${ req.requestId } ] [ Controller ] 请求进入 POST /dev/argv-version`);
    const { uid } = req.token_payload.data;
    let argvVersion;
    // 接收参数
    try {
        argvVersion = new ArgvVersion(req.body);
        argvVersion.content = JSON.stringify(argvVersion.content);  // 参数列表在存储时需要转换成字符串再存入数据库
    } catch (err) {
        return res.status(400).json(new Response().fail(400, "参数版本信息有误"));
    }
    console.log(`[ INFO ] [ req-id = ${ req.requestId } ] [ Controller ] 参数版本信息 ${ JSON.stringify(argvVersion) }`);
    // 进入业务层
    devService.addArgvVersion(req.requestId, uid, argvVersion).then(result => {
        console.log(`[ RESPONSE ] [ req-id = ${ req.requestId } ] [ Controller ] 添加参数版本响应 ${ JSON.stringify(result) }`);
        return res.status(result.code).json(result);
    }).catch(err => {
        console.log(`[ ERROR ] [ req-id = ${ req.requestId } ] [ Controller ] 添加参数版本时服务器内部错误 ${ err.message }`);
        return res.status(500).json(new Response().fail(500, '服务器内部错误'));
    });
});

/**
 * @apiName 获取所有参数版本
 */
router.get('/argvVersionList', function (req, res, next) {
    console.log(`[ REQUEST ] [ req-id = ${ req.requestId } ] [ Controller ] 请求进入 GET /dev/argvVersionList`);
    const { uid } = req.token_payload.data;
    // 进入业务层
    devService.getAllArgvVersion(req.requestId, uid).then(result => {
        console.log(`[ RESPONSE ] [ req-id = ${ req.requestId } ] [ Controller ] 获取所有参数版本响应 ${ JSON.stringify(result) }`);
        return res.status(result.code).json(result);
    }).catch(err => {
        console.log(`[ ERROR ] [ req-id = ${ req.requestId } ] [ Controller ] 获取所有参数版本时服务器内部错误 ${ err.message }`);
        return res.status(500).json(new Response().fail(500, '服务器内部错误'));
    });
});

/**
 * @apiName 获取参数版本详情接口
 */
router.get('/argv-version', function (req, res, next) {
    console.log(`[ REQUEST ] [ req-id = ${ req.requestId } ] [ Controller ] 请求进入 GET /dev/argv-version`);
    const { uid } = req.token_payload.data;
    let argvVersion;
    // 接收参数
    try {
        argvVersion = new ObjId(req.query.id);
    } catch (err) {
        return res.status(400).json(new Response().fail(400, "参数版本Id有误"));
    }
    console.log(`[ INFO ] [ req-id = ${ req.requestId } ] [ Controller ] 参数版本信息 ${ JSON.stringify(argvVersion) }`);
    // 进入业务层
    devService.getArgvVersionInfo(req.requestId, uid, argvVersion).then(result => {
        console.log(`[ RESPONSE ] [ req-id = ${ req.requestId } ] [ Controller ] 获取参数版本详情响应 ${ JSON.stringify(result) }`);
        return res.status(result.code).json(result);
    }).catch(err => {
        console.log(`[ ERROR ] [ req-id = ${ req.requestId } ] [ Controller ] 获取参数版本详情时服务器内部错误 ${ err.message }`);
        return res.status(500).json(new Response().fail(500, '服务器内部错误'));
    });
});

/**
 * @apiName 复制创建参数版本接口
 */
router.post('/copyCreateArgvVersion', function (req, res, next) {
    console.log(`[ REQUEST ] [ req-id = ${ req.requestId } ] [ Controller ] 请求进入 POST /dev/copyCreateArgvVersion`);
    const { uid } = req.token_payload.data;
    let argvVersion;
    // 接收参数
    try {
        argvVersion = new ArgvVersionCopy(req.body);
    } catch (err) {
        return res.status(400).json(new Response().fail(400, "参数版本信息有误"));
    }
    console.log(`[ INFO ] [ req-id = ${ req.requestId } ] [ Controller ] 参数版本信息 ${ JSON.stringify(argvVersion) }`);
    // 进入业务层
    devService.copyCreateArgvVersion(req.requestId, uid, argvVersion).then(result => {
        console.log(`[ RESPONSE ] [ req-id = ${ req.requestId } ] [ Controller ] 复制创建参数版本响应 ${ JSON.stringify(result) }`);
        return res.status(result.code).json(result);
    }).catch(err => {
        console.log(`[ ERROR ] [ req-id = ${ req.requestId } ] [ Controller ] 复制创建参数版本时服务器内部错误 ${ err.message }`);
        return res.status(500).json(new Response().fail(500, '服务器内部错误'));
    });
});

/**
 * @apiName 查看有多少设备使用了该参数版本接口
 */
router.get('/argv-version-used', function (req, res, next) {
    console.log(`[ REQUEST ] [ req-id = ${ req.requestId } ] [ Controller ] 请求进入 GET /dev/argv-version-used`);
    const { uid } = req.token_payload.data;
    let argvVersion;
    // 接收参数
    try {
        argvVersion = new ObjId(req.query.id).id;
    } catch (err) {
        return res.status(400).json(new Response().fail(400, "参数版本id有误"));
    }
    console.log(`[ INFO ] [ req-id = ${ req.requestId } ] [ Controller ] 参数版本id ${ JSON.stringify(argvVersion) }`);
    // 进入业务层
    devService.getArgvVersionUsed(req.requestId, uid, argvVersion).then(result => {
        console.log(`[ RESPONSE ] [ req-id = ${ req.requestId } ] [ Controller ] 查看参数版本使用情况响应 ${ JSON.stringify(result) }`);
        return res.status(result.code).json(result);
    }).catch(err => {
        console.log(`[ ERROR ] [ req-id = ${ req.requestId } ] [ Controller ] 查看参数版本使用情况时服务器内部错误 ${ err.message }`);
        return res.status(500).json(new Response().fail(500, '服务器内部错误'));
    });
});

/**
 * @apiName 删除参数版本接口
 */
router.delete('/argv-version', function (req, res, next) {
    console.log(`[ REQUEST ] [ req-id = ${ req.requestId } ] [ Controller ] 请求进入 DELETE /dev/argv-version`);
    const { uid } = req.token_payload.data;
    let argvVersion;
    // 接收参数
    try {
        argvVersion = new ObjId(req.query.id).id;
    } catch (err) {
        return res.status(400).json(new Response().fail(400, "参数版本id有误"));
    }
    console.log(`[ INFO ] [ req-id = ${ req.requestId } ] [ Controller ] 参数版本id ${ JSON.stringify(argvVersion) }`);
    // 进入业务层
    devService.deleteArgvVersion(req.requestId, uid, argvVersion).then(result => {
        console.log(`[ RESPONSE ] [ req-id = ${ req.requestId } ] [ Controller ] 删除参数版本响应 ${ JSON.stringify(result) }`);
        return res.status(result.code).json(result);
    }).catch(err => {
        console.log(`[ ERROR ] [ req-id = ${ req.requestId } ] [ Controller ] 删除参数版本时服务器内部错误 ${ err.message }`);
        return res.status(500).json(new Response().fail(500, '服务器内部错误'));
    });
});

/**
 * @apiName 修改参数版本接口
 */
router.put('/argv-version', function (req, res, next) {
    console.log(`[ REQUEST ] [ req-id = ${ req.requestId } ] [ Controller ] 请求进入 POST /dev/argv-version`);
    const { uid } = req.token_payload.data;
    let argvVersion, id;
    // 接收参数
    try {
        id = new ObjId(req.body.id).id;
    } catch (err) {
        return res.status(400).json(new Response().fail(400, "参数版本Id有误"));
    }
    console.log(`[ INFO ] [ req-id = ${ req.requestId } ] [ Controller ] 参数版本Id ${ JSON.stringify(id) }`);
    try {
        argvVersion = new ArgvVersion(req.body);
        argvVersion.content = JSON.stringify(argvVersion.content);  // 参数列表在存储时需要转换成字符串再存入数据库
    } catch (err) {
        return res.status(400).json(new Response().fail(400, "参数版本信息有误"));
    }
    console.log(`[ INFO ] [ req-id = ${ req.requestId } ] [ Controller ] 参数版本信息 ${ JSON.stringify(argvVersion) }`);
    // 进入业务层
    devService.updateArgvVersion(req.requestId, uid, id, argvVersion).then(result => {
        console.log(`[ RESPONSE ] [ req-id = ${ req.requestId } ] [ Controller ] 修改参数版本响应 ${ JSON.stringify(result) }`);
        return res.status(result.code).json(result);
    }).catch(err => {
        console.log(`[ ERROR ] [ req-id = ${ req.requestId } ] [ Controller ] 修改参数版本时服务器内部错误 ${ err.message }`);
        return res.status(500).json(new Response().fail(500, '服务器内部错误'));
    });
});

/**
 * @apiName 创建设备类型接口
 * @param { string } type     设备类型
 * @param { string } name     设备名称
 * @pseudocode 运行逻辑:
 *                 1. 需要检查设备类型type 是否出现重复 重复则无法创建
 */
router.post('/dev-type', function (req, res, next) {
    console.log(`[ REQUEST ] [ req-id = ${ req.requestId } ] [ Controller ] 请求进入 POST /dev/dev-type`);
    const { uid } = req.token_payload.data;
    let devType;
    // 接收参数
    try {
        devType = new DevType(req.body);
    } catch (err) {
        return res.status(400).json(new Response().fail(400, "设备类型信息有误"));
    }
    console.log(`[ INFO ] [ req-id = ${ req.requestId } ] [ Controller ] 设备类型信息 ${ JSON.stringify(devType) }`);
    // 进入业务层
    devService.addDevType(req.requestId, uid, devType).then(result => {
        console.log(`[ RESPONSE ] [ req-id = ${ req.requestId } ] [ Controller ] 创建设备类型响应 ${ JSON.stringify(result) }`);
        return res.status(result.code).json(result);
    }).catch(err => {
        console.log(`[ ERROR ] [ req-id = ${ req.requestId } ] [ Controller ] 创建设备类型时服务器内部错误 ${ err.message }`);
        return res.status(500).json(new Response().fail(500, '服务器内部错误'));
    });
});

/**
 * @apiName 获取所有设备类型接口
 */
router.get('/devTypeList', function (req, res, next) {
    console.log(`[ REQUEST ] [ req-id = ${ req.requestId } ] [ Controller ] 请求进入 GET /dev/devTypeList`);
    const { uid } = req.token_payload.data;
    // 进入业务层
    devService.getAllDevType(req.requestId, uid).then(result => {
        console.log(`[ RESPONSE ] [ req-id = ${ req.requestId } ] [ Controller ] 获取所有设备类型响应 ${ JSON.stringify(result) }`);
        return res.status(result.code).json(result);
    }).catch(err => {
        console.log(`[ ERROR ] [ req-id = ${ req.requestId } ] [ Controller ] 获取所有设备类型时服务器内部错误 ${ err.message }`);
        return res.status(500).json(new Response().fail(500, '服务器内部错误'));
    });
});

/**
 * @apiName 修改设备类型接口
 */
router.put('/dev-type', function (req, res, next) {
    console.log(`[ REQUEST ] [ req-id = ${ req.requestId } ] [ Controller ] 请求进入 PUT /dev/dev-type`);
    const { uid } = req.token_payload.data;
    let devTypeId;
    let devType;
    // 接收参数
    try {
        devTypeId = new ObjId(req.body.id).id;
    } catch (err) {
        return res.status(400).json(new Response().fail(400, "设备类型Id有误"));
    }
    console.log(`[ INFO ] [ req-id = ${ req.requestId } ] [ Controller ] 设备类型Id ${ JSON.stringify(devTypeId) }`);
    try {
        devType = new DevType(req.body);
    } catch (err) {
        return res.status(400).json(new Response().fail(400, "设备类型信息有误"));
    }
    console.log(`[ INFO ] [ req-id = ${ req.requestId } ] [ Controller ] 设备类型信息 ${ JSON.stringify(devType) }`);
    // 进入业务层
    devService.updateDevType(req.requestId, uid, devTypeId, devType).then(result => {
        console.log(`[ RESPONSE ] [ req-id = ${ req.requestId } ] [ Controller ] 修改设备类型响应 ${ JSON.stringify(result) }`);
        return res.status(result.code).json(result);
    }).catch(err => {
        console.log(`[ ERROR ] [ req-id = ${ req.requestId } ] [ Controller ] 修改设备类型时服务器内部错误 ${ err.message }`);
        return res.status(500).json(new Response().fail(500, '服务器内部错误'));
    });
});

/**
 * @apiName 查看设备类型使用情况接口
 */
router.get('/devType-used', function (req, res, next) {
    console.log(`[ REQUEST ] [ req-id = ${ req.requestId } ] [ Controller ] 请求进入 GET /dev/devType-used`);
    const { uid } = req.token_payload.data;
    let devType;
    // 接收参数
    try {
        devType = new ObjId(req.query.id).id;
    } catch (err) {
        return res.status(400).json(new Response().fail(400, "设备类型id有误"));
    }
    console.log(`[ INFO ] [ req-id = ${ req.requestId } ] [ Controller ] 设备类型id ${ JSON.stringify(devType) }`);
    // 进入业务层
    devService.getDevTypeUsed(req.requestId, uid, devType).then(result => {
        console.log(`[ RESPONSE ] [ req-id = ${ req.requestId } ] [ Controller ] 查看设备类型使用情况响应 ${ JSON.stringify(result) }`);
        return res.status(result.code).json(result);
    }).catch(err => {
        console.log(`[ ERROR ] [ req-id = ${ req.requestId } ] [ Controller ] 查看设备类型使用情况时服务器内部错误 ${ err.message }`);
        return res.status(500).json(new Response().fail(500, '服务器内部错误'));
    });
});

/**
 * @apiName 删除设备类型接口
 */
router.delete('/devType', function (req, res, next) {
    console.log(`[ REQUEST ] [ req-id = ${ req.requestId } ] [ Controller ] 请求进入 DELETE /dev/devType`);
    const { uid } = req.token_payload.data;
    let devType;
    // 接收参数
    try {
        devType = new ObjId(req.query.id).id;
    } catch (err) {
        return res.status(400).json(new Response().fail(400, "设备类型id有误"));
    }
    console.log(`[ INFO ] [ req-id = ${ req.requestId } ] [ Controller ] 设备类型id ${ JSON.stringify(devType) }`);
    // 进入业务层
    devService.deleteDevType(req.requestId, uid, devType).then(result => {
        console.log(`[ RESPONSE ] [ req-id = ${ req.requestId } ] [ Controller ] 删除设备类型响应 ${ JSON.stringify(result) }`);
        return res.status(result.code).json(result);
    }).catch(err => {
        console.log(`[ ERROR ] [ req-id = ${ req.requestId } ] [ Controller ] 删除设备类型时服务器内部错误 ${ err.message }`);
        return res.status(500).json(new Response().fail(500, '服务器内部错误'));
    });
});

/**
 * @apiName 创建设备接口
 * @param { string } type             设备类型
 * @param { string } version          参数版本
 * @param { int }    departmentID     科室ID - 默认归属于顺元公司
 * @param { string } devID            设备ID - 需要用户手动填写
 * @param { string } devPswd          设备工程密码
 * @pseudocode 运行逻辑:
 *                 1. 需要检查设备类型type 是否出现重复 重复则无法创建
 */
router.post('/dev', function (req, res, next) {
    console.log(`[ REQUEST ] [ req-id = ${ req.requestId } ] [ Controller ] 请求进入 POST /dev/dev`);
    const { uid } = req.token_payload.data;
    let dev;
    // 接收设备信息
    try {
        dev = new Device(req.body);
    } catch (err) {
        return res.status(400).json(new Response().fail(400, "设备信息有误"));
    }
    console.log(`[ INFO ] [ req-id = ${ req.requestId } ] [ Controller ] 设备信息 ${ JSON.stringify(dev) }`);
    // 进入业务层
    devService.addDevice(req.requestId, uid, dev).then(result => {
        console.log(`[ RESPONSE ] [ req-id = ${ req.requestId } ] [ Controller ] 添加设备响应 ${ JSON.stringify(result) }`);
        return res.status(result.code).json(result);
    }).catch(err => {
        console.log(`[ ERROR ] [ req-id = ${ req.requestId } ] [ Controller ] 添加设备时服务器内部错误 ${ err.message }`);
        return res.status(500).json(new Response().fail(500, '服务器内部错误'));
    });
});

/**
 * @apiName 获取所有设备接口
 */
router.get('/devList', function (req, res, next) {
    console.log(`[ REQUEST ] [ req-id = ${ req.requestId } ] [ Controller ] 请求进入 GET /dev/devList`);
    const { uid } = req.token_payload.data;
    // 进入业务层
    devService.getDeviceList(req.requestId, uid).then(result => {
        console.log(`[ RESPONSE ] [ req-id = ${ req.requestId } ] [ Controller ] 获取所有设备响应 ${ JSON.stringify(result) }`);
        return res.status(result.code).json(result);
    }).catch(err => {
        console.log(`[ ERROR ] [ req-id = ${ req.requestId } ] [ Controller ] 获取所有设备时服务器内部错误 ${ err.message }`);
        return res.status(500).json(new Response().fail(500, '服务器内部错误'));
    });
});

/**
 * @apiName 获取设备信息详情接口
 */
router.get('/devInfo', function (req, res, next) {
    console.log(`[ REQUEST ] [ req-id = ${ req.requestId } ] [ Controller ] 请求进入 GET /dev/devInfo`);
    const { uid } = req.token_payload.data;
    let id;
    try {
        id = new ObjId(req.query.id).id;
    } catch (err) {
        return res.status(400).json(new Response().fail(400, "设备Id有误"));
    }
    // 进入业务层
    devService.getDeviceInfo(req.requestId, uid, id).then(result => {
        console.log(`[ RESPONSE ] [ req-id = ${ req.requestId } ] [ Controller ] 获取设备详细信息响应 ${ JSON.stringify(result) }`);
        return res.status(result.code).json(result);
    }).catch(err => {
        console.log(`[ ERROR ] [ req-id = ${ req.requestId } ] [ Controller ] 获取设备详细信息时服务器内部错误 ${ err.message }`);
        return res.status(500).json(new Response().fail(500, '服务器内部错误'));
    });
});

/**
 * @apiName 删除设备接口
 */
router.delete('/dev', function (req, res, next) {
    console.log(`[ REQUEST ] [ req-id = ${ req.requestId } ] [ Controller ] 请求进入 DELETE /dev/dev`);
    const { uid } = req.token_payload.data;
    let id;
    try {
        id = new ObjId(req.query.id).id;
    } catch (err) {
        return res.status(400).json(new Response().fail(400, "设备Id有误"));
    }
    // 进入业务层
    devService.deleteDevice(req.requestId, uid, id).then(result => {
        console.log(`[ RESPONSE ] [ req-id = ${ req.requestId } ] [ Controller ] 删除设备响应 ${ JSON.stringify(result) }`);
        return res.status(result.code).json(result);
    }).catch(err => {
        console.log(`[ ERROR ] [ req-id = ${ req.requestId } ] [ Controller ] 删除设备时服务器内部错误 ${ err.message }`);
        return res.status(500).json(new Response().fail(500, '服务器内部错误'));
    });
});

/**
 * @apiName 获取设备参数接口
 */
router.get('/devArgv', function (req, res, next) {
    console.log(`[ REQUEST ] [ req-id = ${ req.requestId } ] [ Controller ] 请求进入 GET /dev/devArgv`);
    const { uid } = req.token_payload.data;
    let id;
    try {
        id = new ObjId(req.query.id).id;
    } catch (err) {
        return res.status(400).json(new Response().fail(400, "设备Id有误"));
    }
    // 进入业务层
    devService.getDeviceArgv(req.requestId, uid, id).then(result => {
        console.log(`[ RESPONSE ] [ req-id = ${ req.requestId } ] [ Controller ] 获取设备参数响应 ${ JSON.stringify(result) }`);
        return res.status(result.code).json(result);
    }).catch(err => {
        console.log(`[ ERROR ] [ req-id = ${ req.requestId } ] [ Controller ] 获取设备参数时服务器内部错误 ${ err.message }`);
        return res.status(500).json(new Response().fail(500, '服务器内部错误'));
    });
});

/**
 * @apiName 操作设备读取参数接口
 */
router.post('/ctrl-readDevArgv', function (req, res, next) {
    console.log(`[ REQUEST ] [ req-id = ${ req.requestId } ] [ Controller ] 请求进入 POST /dev/ctrl-readDevArgv`);
    const { uid } = req.token_payload.data;
    let id;
    try {
        id = new ObjId(req.body.id).id;
    } catch (err) {
        return res.status(400).json(new Response().fail(400, "设备Id有误"));
    }
    // 进入业务层
    devService.ctrlReadArgv(req.requestId, uid, id).then(result => {
        console.log(`[ RESPONSE ] [ req-id = ${ req.requestId } ] [ Controller ] 控制设备读取参数响应 ${ JSON.stringify(result) }`);
        return res.status(result.code).json(result);
    }).catch(err => {
        console.log(`[ ERROR ] [ req-id = ${ req.requestId } ] [ Controller ] 控制设备读取参数时服务器内部错误 ${ err.message }`);
        return res.status(500).json(new Response().fail(500, '服务器内部错误'));
    });
});

/**
 * @apiName 操作设备设置状态接口
 */
router.post('/ctrl-setDevStatus', function (req, res, next) {
    console.log(`[ REQUEST ] [ req-id = ${ req.requestId } ] [ Controller ] 请求进入 POST /dev/ctrl-setDevStatus`);
    const { uid } = req.token_payload.data;
    let id, status;
    try {
        id = new ObjId(req.body.id).id;
        status = new SafeString(req.body.status).value;
        if ([ 'Enable', 'Disable' ].includes(status) == false) {
            throw new TypeError(`Expected Enable/Disable, got ${status}`);
        }
    } catch (err) {
        return res.status(400).json(new Response().fail(400, "设备状态参数有误"));
    }
    // 进入业务层
    devService.ctrlSetStatus(req.requestId, uid, id, status).then(result => {
        console.log(`[ RESPONSE ] [ req-id = ${ req.requestId } ] [ Controller ] 控制设备设置状态响应 ${ JSON.stringify(result) }`);
        return res.status(result.code).json(result);
    }).catch(err => {
        console.log(`[ ERROR ] [ req-id = ${ req.requestId } ] [ Controller ] 控制设备设置状态时服务器内部错误 ${ err.message }`);
        return res.status(500).json(new Response().fail(500, '服务器内部错误'));
    });
});

/**
 * @apiName 操作设备设置密码接口
 */
router.post('/ctrl-setDevPswd', function (req, res, next) {
    console.log(`[ REQUEST ] [ req-id = ${ req.requestId } ] [ Controller ] 请求进入 POST /dev/ctrl-setDevPswd`);
    const { uid } = req.token_payload.data;
    let id, password;
    try {
        id = new ObjId(req.body.id).id;
        password = new SafeString(req.body.password, 1, 50).value;
    } catch (err) {
        return res.status(400).json(new Response().fail(400, "设备密码参数有误"));
    }
    // 进入业务层
    devService.ctrlSetPassword(req.requestId, uid, id, password).then(result => {
        console.log(`[ RESPONSE ] [ req-id = ${ req.requestId } ] [ Controller ] 控制设备设置密码响应 ${ JSON.stringify(result) }`);
        return res.status(result.code).json(result);
    }).catch(err => {
        console.log(`[ ERROR ] [ req-id = ${ req.requestId } ] [ Controller ] 控制设备设置密码时服务器内部错误 ${ err.message }`);
        return res.status(500).json(new Response().fail(500, '服务器内部错误'));
    });
});

/**
 * @apiName 操作设备取消当前操作接口
 */
router.post('/ctrl-cancel', function (req, res, next) {
    console.log(`[ REQUEST ] [ req-id = ${ req.requestId } ] [ Controller ] 请求进入 POST /dev/ctrl-cancel`);
    const { uid } = req.token_payload.data;
    let id;
    try {
        id = new ObjId(req.body.id).id;
    } catch (err) {
        return res.status(400).json(new Response().fail(400, "设备Id有误"));
    }
    // 进入业务层
    devService.ctrlCancel(req.requestId, uid, id).then(result => {
        console.log(`[ RESPONSE ] [ req-id = ${ req.requestId } ] [ Controller ] 控制设备取消当前操作响应 ${ JSON.stringify(result) }`);
        return res.status(result.code).json(result);
    }).catch(err => {
        console.log(`[ ERROR ] [ req-id = ${ req.requestId } ] [ Controller ] 控制设备取消当前操作时服务器内部错误 ${ err.message }`);
        return res.status(500).json(new Response().fail(500, '服务器内部错误'));
    });
});

/**
 * @apiName 修改设备归属接口
 */
router.put('/devDepartment', function (req, res, next) {
    console.log(`[ REQUEST ] [ req-id = ${ req.requestId } ] [ Controller ] 请求进入 PUT /dev/devDepartment`);
    const { uid } = req.token_payload.data;
    let id, departmentId;
    try {
        id = new ObjId(req.body.id).id;
        departmentId = new ObjId(req.body.departmentId).id;
    } catch (err) {
        return res.status(400).json(new Response().fail(400, "修改设备归属参数有误"));
    }
    // 进入业务层
    devService.updateDevDepartment(req.requestId, uid, id, departmentId).then(result => {
        console.log(`[ RESPONSE ] [ req-id = ${ req.requestId } ] [ Controller ] 修改设备归属响应 ${ JSON.stringify(result) }`);
        return res.status(result.code).json(result);
    }).catch(err => {
        console.log(`[ ERROR ] [ req-id = ${ req.requestId } ] [ Controller ] 修改设备归属时服务器内部错误 ${ err.message }`);
        return res.status(500).json(new Response().fail(500, '服务器内部错误'));
    });
});

/**
 * @apiName 修改设备医疗参数接口
 */
router.put('/devInfo', function (req, res, next) {
    console.log(`[ REQUEST ] [ req-id = ${ req.requestId } ] [ Controller ] 请求进入 PUT /dev/devInfo`);
    const { uid } = req.token_payload.data;
    let updateDevInfo;
    try {
        updateDevInfo = new UpdateDevInfo(req.body);
    } catch (err) {
        console.log(err);
        return res.status(400).json(new Response().fail(400, "修改设备医疗参数有误"));
    }
    // 进入业务层
    devService.updateDevInfo(req.requestId, uid, updateDevInfo).then(result => {
        console.log(`[ RESPONSE ] [ req-id = ${ req.requestId } ] [ Controller ] 修改设备医疗参数响应 ${ JSON.stringify(result) }`);
        return res.status(result.code).json(result);
    }).catch(err => {
        console.log(`[ ERROR ] [ req-id = ${ req.requestId } ] [ Controller ] 修改设备医疗参数时服务器内部错误 ${ err.message }`);
        return res.status(500).json(new Response().fail(500, '服务器内部错误'));
    });
});

router.put('/openRate', function (req, res, next) {
    console.log(`[ REQUEST ] [ req-id = ${ req.requestId } ] [ Controller ] 请求进入 PUT /dev/openRate`);
    const { uid } = req.token_payload.data;
    let updateOpenRate;
    try {
        updateOpenRate = new UpdateOpenRate(req.body);
    } catch (err) {
        console.log(err);
        return res.status(400).json(new Response().fail(400, "修改设备开机率参数有误"));
    }
    // 进入业务层
    devService.updateOpenRate(req.requestId, uid, updateOpenRate).then(result => {
        console.log(`[ RESPONSE ] [ req-id = ${ req.requestId } ] [ Controller ] 修改设备开机率响应 ${ JSON.stringify(result) }`);
        return res.status(result.code).json(result);
    }).catch(err => {
        console.log(`[ ERROR ] [ req-id = ${ req.requestId } ] [ Controller ] 修改设备开机率时服务器内部错误 ${ err.message }`);
        return res.status(500).json(new Response().fail(500, '服务器内部错误'));
    });
});

module.exports = router;